﻿''' <summary>
''' Author: BrumbaZ
''' Date: Oct 07 2009
''' </summary>
''' <remarks></remarks>
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.ShowDialog()
        TextBox1.Text = ""
        For index As Integer = 0 To OpenFileDialog1.FileNames.Length - 1
            TextBox1.Text += vbCrLf + OpenFileDialog1.FileNames(index)
        Next
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim tamanho As Integer = Integer.Parse(InputBox("Tamanho em MB"))
        Dim filinfo = New System.IO.FileInfo(OpenFileDialog1.FileNames(0))
        Dim qtd As Integer = (New System.IO.FileInfo(OpenFileDialog1.FileNames(0)).Length) / (tamanho * 1024 * 1024)
        Dim size As Integer = filinfo.Length
        Dim size2 As Integer = New System.IO.FileInfo(OpenFileDialog1.FileNames(0)).Length
        Dim fs As System.IO.FileStream = filinfo.OpenRead()
        Dim lBytes As Long = fs.Length
        Dim fileData(lBytes - 1) As Byte
        Dim count As Integer = 0
        Dim countSaves As Integer = 0
        Dim sr As System.IO.Stream


        fs.Read(fileData, 0, lBytes)

        For index As Integer = 0 To fileData.Length - 1
            If count = 0 Then
                If Not sr Is Nothing Then
                    sr.Close()
                End If
                sr = New System.IO.FileStream(TextBox2.Text + "_" + countSaves.ToString() + ".htway", IO.FileMode.CreateNew)
                countSaves += 1
            End If
            count += 1
            sr.Write(fileData, index, Len(fileData(index)))
            If count >= size2 / qtd Then
                count = 0
            End If
            size = size - 1
        Next

        If Not sr Is Nothing Then
            sr.Close()
        End If
        fs.Close()

        MessageBox.Show("Concluído!")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim finalFile As New System.IO.FileStream(TextBox2.Text, IO.FileMode.Create)
        Dim content As String = ""
        Dim arrOld As System.Array = OpenFileDialog1.FileNames.ToArray()
        Dim indexFileFull As Long = 0
        Dim arr As List(Of String) = retornaOrdenados(arrOld)

        For index As Integer = 0 To arr.Count - 1
            Dim file As New System.IO.FileStream(arr(index).ToString(), IO.FileMode.Open)
            Dim lBytes As Long = file.Length
            Dim fileData(lBytes) As Byte
            Dim bytesRead As Long = file.Read(fileData, 0, lBytes)
            finalFile.Write(fileData, 0, bytesRead)
            indexFileFull += fileData.Length - 1
            file.Close()
        Next

        finalFile.Close()
        MessageBox.Show("Concluído!")

    End Sub

    Private Function retornaOrdenados(ByVal val() As String) As List(Of String)
        Dim correlatos As New List(Of String)
        Dim nomesnumeros As New List(Of NomesNumeros)
        Dim numeros As New List(Of Integer)
        Dim arr As Array

        For index As Integer = 0 To val.Length - 1
            Dim ltr As New NomesNumeros()
            ltr.Numeros = Integer.Parse(val(index).Substring(val(index).IndexOf("_") + 1, val(index).IndexOf(".") - 2 - val(index).IndexOf("_") + 1))
            numeros.Add(Integer.Parse(val(index).Substring(val(index).IndexOf("_") + 1, val(index).IndexOf(".") - 2 - val(index).IndexOf("_") + 1)))
            ltr.Nome = val(index)
            nomesnumeros.Add(ltr)
        Next
        arr = numeros.ToArray()
        Array.Sort(arr)
        For index As Integer = 0 To arr.Length - 1
            For index1 As Integer = 0 To nomesnumeros.Count - 1
                If (nomesnumeros(index1).Numeros = arr(index)) Then
                    correlatos.Add(nomesnumeros(index1).Nome)
                End If
            Next
        Next

        Return correlatos
    End Function


    Class NomesNumeros
        Public Numeros As Integer
        Public Nome As String
    End Class

End Class
